<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <h1>砍价活动进行中</h1>
      <el-form-item>
        <el-input
          v-model="dataForm.key"
          placeholder="参数名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="order()">砍价订单页</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>

      <el-table-column
        prop="goodsName"
        header-align="center"
        align="center"
        label="商品名称"
      >
      </el-table-column>
      <el-table-column
        prop="goodsImg"
        header-align="center"
        align="center"
        label="商品图片"
      >
        <template slot-scope="scope">
          <img :src="scope.row.goodsImg" width="150px" />
        </template>
      </el-table-column>
      <el-table-column
        prop="activityPrice"
        header-align="center"
        align="center"
        label="初始价格"
      >
      </el-table-column>

      <el-table-column
        prop="goodsStock"
        header-align="center"
        align="center"
        label="库存剩余"
      >
      </el-table-column>
      <el-table-column
        prop="startTime"
        header-align="center"
        align="center"
        label="活动开始时间"
      >
      </el-table-column>
      <el-table-column
        prop="endTime"
        header-align="center"
        align="center"
        label="活动结束时间"
      >
      </el-table-column>
      <el-table-column
        prop="activityStatus"
        header-align="center"
        align="center"
        label="活动状态"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="launch(scope.row)"
            >发起砍价</el-button
          >
          <el-button
            type="primary"
            size="small"
            @click="bargainrecord(scope.row.activityHid)"
            >砍价记录</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>

    <el-dialog :close-on-click-modal="false" :visible.sync="visible">
      <el-form :inline="true" :model="dataForm1"> </el-form>
      <el-table
        :data="dataList1"
        border
        v-loading="dataListLoading1"
        @selection-change="selectionChangeHandle1"
        style="width: 100%"
      >
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50"
        >
        </el-table-column>

        <el-table-column
          prop="userId"
          header-align="center"
          align="center"
          label="用户id"
        >
        </el-table-column>
        <el-table-column
          prop="goodsImg"
          header-align="center"
          align="center"
          label="商品图片"
        >
          <template slot-scope="scope">
            <img :src="scope.row.goodsImg" width="150px" />
          </template>
        </el-table-column>
        <el-table-column
          prop="bargainBprice"
          header-align="center"
          align="center"
          label="已砍价格"
        >
        </el-table-column>
        <el-table-column
          prop="goodsPrice"
          header-align="center"
          align="center"
          label="原价"
        >
        </el-table-column>
        <el-table-column
          prop="count"
          header-align="center"
          align="center"
          label="已砍刀数"
        >
        </el-table-column>
        <el-table-column
          prop="bargainPrice"
          header-align="center"
          align="center"
          label="现价"
        >
        </el-table-column>
        <el-table-column
          prop="recordStatus"
          header-align="center"
          align="center"
          label="状态"
        >
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{
              options[scope.row.recordStatus].label
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作"
        >
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="sizeChangeHandle1"
        @current-change="currentChangeHandle1"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
    </el-dialog>
  </div>
</template>

<script>
import AddOrUpdate from "./bargainactivity-add-or-update";
import moment from "moment";
export default {
  data() {
    return {
      options: [
        {
          recordStatus: 0,
          label: "砍价失败",
        },
        {
          recordStatus: 1,
          label: "砍价成功",
        }
      ],
      recordStatus: "",
      end_date: "", // 截止时间
      start_date: "", // 截止时间
      days: 0,
      hours: 0,
      minutes: 0,
      seconds: 0,
      residue: 0,
      sdays: 0,
      shours: 0,
      sminutes: 0,
      sseconds: 0,
      sresidue: 0,
      activityStatus: "",
      visible: false,
      dataForm: {
        key: "",
        horderId: 0,
        activityHid: "",
        goodsId: "",
        goodsName: "",
        horderAdd: "",
        goodsCount: "",
        goodsPrice: "",
        horderYprice: "",
        horderPrice: "",
        horderStatus: "",
        payDate: "",
        createDate: "",
        userId: "",
        goodsImg: "",
      },
      dataForm1: {
        recordId: 0,
        activityHid: "",
        goodsId: "",
        userId: "",
        bargainBprice: "",
        goodsPrice: "",
        count: "",
        bargainPrice: "",
        recordStatus: "",
      },
      dataList: [],
      dataList1: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListLoading1: false,
      dataListSelections: [],
      dataListSelections1: [],
      addOrUpdateVisible: false,
      baseUrl: "http://localhost:88/api",
    };
  },
  components: {
    AddOrUpdate,
  },
  created() {
    this.dataForm.userId = localStorage.getItem("userId");
    this.dataForm.userName = localStorage.getItem("userName");
    this.getDataList();
    setInterval(() => {
      this.getResidueDate();
    }, 1000);
  },
  methods: {
    order() {
      this.$router.push("/bargainorder");
    },
    launch(bargain) {
      this.dataForm.horderId = bargain.horderId;
      this.dataForm.activityHid = bargain.activityHid;
      this.dataForm.goodsId = bargain.goodsId;
      this.dataForm.goodsName = bargain.goodsName;
      this.dataForm.goodsCount = 1;
      this.dataForm.goodsPrice = bargain.activityPrice;
      this.dataForm.goodsImg = bargain.goodsImg;
      this.$axios
        .post(`${this.baseUrl}/kms/bargainorder/launch`, this.dataForm)
        .then(({ data }) => {
          if (data && data.code === 0) {
            this.$router.push("/bargainorder");
          } else {
            alert(data.msg);
          }
          this.dataListLoading = false;
        });
    },
    bargainrecord(activityHid) {
      this.visible = true;
      this.$axios
        .get(
          `${this.baseUrl}/kms/bargainrecord/list?activityHid=${activityHid}`
        )
        .then(({ data }) => {
          if (data && data.code === 0) {
            this.dataList1 = data.page.list;
            this.totalPage = data.page.totalCount;
          } else {
            this.dataList1 = [];
            this.totalPage = 0;
          }
          this.dataListLoading = false;
        });
    },
    // 每页数
    sizeChangeHandle1(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.bargainrecord();
    },
    // 当前页
    currentChangeHandle1(val) {
      this.pageIndex = val;
      this.bargainrecord();
    },
    // 多选
    selectionChangeHandle1(val) {
      this.dataListSelections1 = val;
    },
    getResidueDate() {
      this.residue = new Date(this.end_date).getTime() - new Date();
      this.days = this.addZero(Math.floor(this.residue / 1000 / 60 / 60 / 24)); //天
      this.hours = this.addZero(
        Math.floor((this.residue / 1000 / 60 / 60) % 24)
      ); //时
      this.minutes = this.addZero(Math.floor((this.residue / 1000 / 60) % 60)); //分
      this.seconds = this.addZero(Math.floor((this.residue / 1000) % 60)); //秒

      this.sresidue = new Date(this.start_date).getTime() - new Date();
      this.sdays = this.addZero(
        Math.floor(this.sresidue / 1000 / 60 / 60 / 24)
      ); //天
      this.shours = this.addZero(
        Math.floor((this.sresidue / 1000 / 60 / 60) % 24)
      ); //时
      this.sminutes = this.addZero(
        Math.floor((this.sresidue / 1000 / 60) % 60)
      ); //分
      this.sseconds = this.addZero(Math.floor((this.sresidue / 1000) % 60)); //秒
    },
    start() {
      // let _this = this;
    },
    addZero(d) {
      return parseInt(d) < 10 ? "0" + d : d;
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$axios
        .get(`${this.baseUrl}/kms/bargainactivity/list`)
        .then(({ data }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list;
            this.totalPage = data.page.totalCount;
            var time = moment(new Date()).format("YYYY-MM-DD HH:mm:ss");
            console.log(time);
            console.log(this.dataList[0].endTime);

            for (var i = 0; i < this.dataList.length; i++) {
              if (this.dataList[i].endTime < time) {
                this.dataList[i].activityStatus = "活动已结束";
              } else if (
                this.dataList[i].endTime > time &&
                this.dataList[i].startTime < time
              ) {
                this.dataList[i].activityStatus = "正在秒杀";
              } else if (this.dataList[i].startTime > time) {
                this.dataList[i].activityStatus = "等待秒杀";
              }
            }
          } else {
            this.dataList = [];
            this.totalPage = 0;
          }
          this.dataListLoading = false;
        });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.activityHid;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/kms/bargainactivity/delete"),
          method: "post",
          data: this.$http.adornData(ids, false),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
  },
};
</script>
